package com.guigusuqi.commonutils.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BelongCalendarUtils
{
    /**
     * 判断当前时间是否在时间段内
     * @param beginTimeStr 起始时间，格式应与format匹配 如"2022-08-01 08:12:08"
     * @param endTimeStr 结束时间，格式应与format匹配
     * @param format 时间格式，如 yyyy-MM-dd HH:mm:ss
     * @return boolean
     */
    public static boolean belongCalendar(String beginTimeStr, String endTimeStr, String format) {

        SimpleDateFormat df = new SimpleDateFormat(format);
        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            nowTime = df.parse(df.format(new Date()));
            beginTime = df.parse(beginTimeStr);
            endTime = df.parse(endTimeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
}
